<template>
  <!-- <div>
    <a href="/">返回首页</a>
    <br />
    <h1>学习——vue版用户登录</h1>
  </div> -->

  <div class="main">
    <el-card>
      <template #header> 用户登录 </template>

      <div>
        <el-form>
          <el-form-item>
            <el-input v-model="user.username" placeholder="用户名">
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-input show-password v-model="user.password" placeholder="密码">
              <template #prefix>
                <el-icon><Lock /></el-icon> </template
            ></el-input>
          </el-form-item>
        </el-form>
      </div>

      <template #footer>
        <el-button v-loading="loading" @click="login" type="success">
          <el-icon style="margin-right: 0.3em"><User /></el-icon>
          登录</el-button
        >
        <el-button @click="reg" type="primary">
          <el-icon style="margin-right: 0.3em"><Back /></el-icon>
          注册新用户</el-button
        >
      </template>
    </el-card>
  </div>
</template>

<script setup>
import { api } from '../../js/ajax'
import { tools } from '../../js/tools'
import { ElMessageBox } from 'element-plus'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const loading = ref(false)
const user = ref({
  username: '33',
  password: '3254643486',
})

const login = () => {
  loading.value = true
  api.post('/auth', user.value, data => {
    loading.value = false
    if (data.success) {
      router.push('/user/Main')
      return
    }
    ElMessageBox.alert(data.message, '登录失败')
  })
}

const reg = () => {
  router.push('/user/Reg')
}
</script>

<style scoped>
.main {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
